/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bccomp;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Scanner;

/**
 *
 * @author adam
 */
public class ByteParser {

    public static void generateBytecode(String code, OutputStream os, HashMap<String, Integer> map) throws FileNotFoundException, IOException {


        Scanner sc = new Scanner(code);

        byte b;
        String token;
        while (sc.hasNext()) {
            token = sc.next();

            if (token.equals("goto")) {
                b = StringToByte.convertToByte(token);
                os.write(b);
                String lab = sc.next();
                Integer position = map.get(lab);
                os.write(intToByteArray(position));
            } else if (token.equals("if")) {

                b = StringToByte.convertToByte(token);
                os.write(b);

                b = StringToByte.convertToByte(sc.next());
                os.write(b);

                String lab = sc.next();
                Integer position = map.get(lab);
                os.write(intToByteArray(position));

            } else if (token.equals("invoke")) {
                b = StringToByte.convertToByte(token);
                os.write(b);
                String lab = sc.next();
                Integer position = map.get(lab + "()");
                os.write(intToByteArray(position));
            } else {
                b = StringToByte.convertToByte(token);

                if (b == (byte) 0xFF) {
                    if (token.startsWith("\"")) {
                       // System.out.println("inside");
                        String s = "";
                        int a = 0;
                        //System.out.print("TOKEN"+token.length()+"TOKEN");
                        while(!((token.endsWith("\""))&& (a>0 || token.length()>1))){
                            //System.out.println("insideWHILE");
                            s = s.concat(token + " ");
                            a++;
                            token = sc.next();
                        }
                        s = s.concat(token);
                        s = s.replaceAll("\"", "");
                        //System.out.println(s);
                        os.write(intToByteArray(s.getBytes().length));
                        os.write(s.getBytes());


                    } else {
                        Integer integer = Integer.valueOf(token);
                        os.write(intToByteArray(integer));
                    }
                } else {
                    os.write(b);
                }
            }
        }

    }

    public static byte[] intToByteArray(int value) {
        return new byte[]{
                    (byte) (value >>> 24),
                    (byte) (value >>> 16),
                    (byte) (value >>> 8),
                    (byte) value};
    }
}
